package com.easonji.attendance.service.impl;

import com.easonji.attendance.model.Holiday;
import com.easonji.attendance.mapper.HolidayMapper;
import com.easonji.attendance.service.IHolidayService;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.page.PageInfo;
import com.easonji.common.util.StringUtils;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.factory.PageFactory;

import com.easonji.core.shiro.ShiroUser;
import com.easonji.core.shiro.util.ShiroUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import com.alibaba.fastjson.JSONObject;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 节假日表 服务实现类
 * </p>
 *
 * @author easonji
 * @since 2018-09-22
 */
@Service
public class HolidayServiceImpl extends AbstractBaseService<Holiday> implements IHolidayService {

    @Resource
    private HolidayMapper holidayMapper;

    @Override
    protected JSONObject warp2Json(Holiday holiday) {
       JSONObject result = JsonUtils.parseObject(holiday);

       return result;
    }


    @Override
    public Object queryList(BaseQuery baseQuery){

        // 分页信息
        Page<Holiday> page = new PageFactory<Holiday>().defaultPage();
        // 查询条件
        Wrapper<Holiday> wrapper = new EntityWrapper<>();

        String beginTime = baseQuery.getBeginTime();
        if (StringUtils.isNotEmpty(beginTime)) {
            wrapper.ge("holi_date", beginTime);
        }
        String endTime = baseQuery.getEndTime();
        if (StringUtils.isNotEmpty(endTime)) {
            wrapper.le("holi_date", endTime);
        }
        String condition = baseQuery.getCondition();
        if (StringUtils.isNotEmpty(condition)) {
            wrapper.like("holi_name", condition);
        }

        List<Holiday> holidays = holidayMapper.selectPage(page, wrapper);

        return new PageInfo(warp(holidays), page.getTotal());
    }

    @Override
    public Object queryRowById(Integer holiId) {
        Holiday holiday = holidayMapper.selectById(holiId);
        return warp(holiday);
    }

    @Override
    public Integer save(Holiday holiday) {
        ShiroUser shiroUser = ShiroUtils.getUser();
        holiday.setHoliCreateUser(shiroUser.getUserId());
        holiday.setHoliCreateDate(new Date());
        holiday.setHoliUpdateUser(shiroUser.getUserId());
        holiday.setHoliUpdateDate(new Date());

        return holidayMapper.insert(holiday);
    }

    @Override
    public Integer update(Holiday holidayForm) {
        ShiroUser shiroUser = ShiroUtils.getUser();
        holidayForm.setHoliUpdateUser(shiroUser.getUserId());
        holidayForm.setHoliUpdateDate(new Date());
        Holiday holiday = new Holiday();
        BeanUtils.copyProperties(holidayForm,holiday);
        return holidayMapper.updateById(holiday);
    }

    @Override
    public Integer delete(Integer holiId) {
        Wrapper<Holiday> wrapper = new EntityWrapper<>();
        wrapper.eq("holi_id", holiId);
        return holidayMapper.delete(wrapper);
    }


}
